import Server from "@/config/server";
import request from '@/utils/request';
import router from '@/router';
import { showErrors, showError, showSuccess } from '@/utils/showMsg';

const state = {
  requestCode : false,
  dataLists: [],
  Fields: [],
  deviceTypeData: [],
  itemDetail: {},
};
const actions = {
  //列表
  getLists({commit}, page) {
    request.get(Server.action.alarmTrigger_list, { params: page })
    .then(response => {
      console.log(response);
        if (response.data.errorCode === 0) {
            commit("setLists", response.data.result.list);
            let fields = response.data.result.fields;
            fields.push({
                title: '操作',
                align: 'center',
                width: 120,
                scopedSlots: { customRender: 'action' },
                fixed: 'right',
            })
            fields[0].width = 50
            fields.forEach(function (item, i) {
              item.dataIndex = item.key
              item.align = 'center';
              // if(i < 6) {
              //   item.width = item.width || 100
              // }
              if(i < fields.length-2) {
                item.width = item.width || 140
              }
              if(item.key == "status_name") {
                item.scopedSlots = { customRender: 'status_name' }
              }
            })
            commit("setFields", fields);
        } else {

        }
    })
  },
  //添加
  dataAdd({commit}, data) {
    commit("setErrorCode", false);
    let formData = data
    formData.contact_ids = formData.contact_ids.join(',')
    formData.type = formData.type.join(',')
    request.post(Server.action.alarmTrigger_add, formData )
    .then(response => {
      console.log(response);
      commit("setErrorCode", response.data.errorCode);
      if (response.data.errorCode === 0) {
        showSuccess(response.data.result)
        router.push({
          path: '/alert/Trigger',
        })
      } else if(response.status === 422) {
        showErrors( response.data.errors )
      } else {
        showError( response.data.message )
      }
    })
  },
  //编辑
  dataEdit({commit}, data) {
    commit("setErrorCode", false);
    let formData = data
    formData.contact_ids = formData.contact_ids.join(',')
    formData.type = formData.type.join(',')
    console.log(formData);
    request.post(Server.action.alarmTrigger_update, formData )
    .then(response => {
      console.log(response);
      commit("setErrorCode", response.data.errorCode);
      if (response.data.errorCode === 0) {
        showSuccess(response.data.result)
        router.push({
          path: '/alert/Trigger',
        })
      } else {
        showError( response.data.message )
      }
    })
  },
  //触发器开关
  dataEditSwitch({commit}, data) {
    request.post(Server.action.alarmTrigger_update, data )
    .then(response => {
      console.log(response);
      if (response.data.errorCode === 0) {
        showSuccess(response.data.result)
        router.push({
          path: '/alert/Trigger',
        })
      } else {
        showError( response.data.message )
      }
    })
  },
  // ID删除 > 参数为：url/id
  dataDel({commit}, id) {
    request.get(Server.action.alarmTrigger_delete + id)
    .then(response => {
      console.log(response);
      if (response.data.errorCode === 0) {
        showSuccess(response.data.result)
      } else {
        showError( response.data.message )
      }
    })
  },
  // 详情
  dataDetail({commit}, id) {
    request.get(Server.action.alarmTrigger_detail + id)
    .then(response => {
      console.log(response);
      if (response.data.errorCode === 0) {
        commit("setDetail", response.data.result);
        // showSuccess(response.data.result)
      } else {
        showError( response.data.message )
      }
    })
  },
  // 所有设备类型信息
  deviceTypeAll({commit}) {
    request.get(Server.action.equipmentType_all)
    .then(response => {
      console.log(response);
      if (response.data.errorCode === 0) {
        commit("setdeviceTypeAll", response.data.result);
      } else {
        showError( response.data.message )
      }
    })
  },
};
const mutations = {
  setErrorCode(state, text) {
	    state.requestCode = text;
	},
	setLists(state, lists) {
	    state.dataLists = lists;
	},
  setFields(state, lists) {
	    state.Fields = lists;
	},
  setdeviceTypeAll(state, data) {
    state.deviceTypeData = data;
  },
  setDetail(state, data) {
    state.itemDetail = data;
},
};
export default {
    namespaced: true,
    state,
    actions,
    mutations
};
